{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "horizontal-desperate",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "personalized-diversity",
   "metadata": {},
   "outputs": [],
   "source": [
    "class ResBlock(nn.Module):\n",
    "    def __init__(self,num_channels=16):\n",
    "        super(ResBlock,self).__init__()\n",
    "        \n",
    "        self.conv1 = nn.Conv2d(num_channels,num_channels,kernel_size=3,stride=1,padding=1)\n",
    "        self.bn1 = nn.BatchNorm2d(num_channels)\n",
    "        self.leakyrelu = nn.LeakyReLU(negative_slope=0.2,inplace=True)\n",
    "        \n",
    "        self.conv2 = nn.Conv2d(num_channels,num_channels,kernel_size=3,stride=1,padding=1)\n",
    "        self.bn2 = nn.BatchNorm2d(num_channels)\n",
    "    \n",
    "    def forward(self,x):\n",
    "        residual = x\n",
    "        \n",
    "        out = self.conv1(x)\n",
    "        out = self.bn1(out)\n",
    "        \n",
    "        out = self.leakyrelu(out)\n",
    "        \n",
    "        out = self.conv2(out)\n",
    "        out = self.bn2(out)\n",
    "        out += residual\n",
    "        \n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "gorgeous-holiday",
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_block(r,n):\n",
    "    residual = []\n",
    "    \n",
    "    for i in range(r):\n",
    "        block = ResBlock(num_channels=n)\n",
    "        residual.append(block)\n",
    "    \n",
    "    return nn.Sequential(*residual)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "pointed-hundred",
   "metadata": {},
   "outputs": [],
   "source": [
    "class ResizingNetwork(nn.Module):\n",
    "    def __init__(self,r=1, n=16):\n",
    "        super(ResizingNetwork, self).__init__()\n",
    "        \n",
    "        self.conv1 = nn.Conv2d(in_channels=3,out_channels=n,kernel_size=7,stride=1,padding=3)\n",
    "        self.leakyrelu1 = nn.LeakyReLU(negative_slope=0.2,inplace=True)\n",
    "        \n",
    "        self.conv2 = nn.Conv2d(n,n,kernel_size=1,stride=1)\n",
    "        self.leakyrelu2 = nn.LeakyReLU(negative_slope=0.2,inplace=True)\n",
    "        self.bn1 = nn.BatchNorm2d(n)\n",
    "        \n",
    "        \n",
    "        self.resblock = make_block(r,n)\n",
    "        \n",
    "        \n",
    "        self.conv3 = nn.Conv2d(n,n,kernel_size=3,stride=1,padding=1)\n",
    "        self.bn2 = nn.BatchNorm2d(n)\n",
    "        \n",
    "        self.conv4 = nn.Conv2d(n,out_channels=3,kernel_size=7,stride=1,padding=3)\n",
    "        \n",
    "    \n",
    "    def forward(self,x):\n",
    "        \n",
    "        residual = F.interpolate(x,scale_factor=0.5,mode='bilinear')\n",
    "        \n",
    "        out = self.conv1(x)\n",
    "        out = self.leakyrelu1(out)\n",
    "        \n",
    "        out = self.conv2(out)\n",
    "        out = self.leakyrelu2(out)\n",
    "        out = self.bn1(out)\n",
    "        \n",
    "        out_residual = F.interpolate(out,scale_factor=0.5,mode='bilinear')\n",
    "        \n",
    "        out = self.resblock(out_residual)\n",
    "        \n",
    "        out = self.conv3(out)\n",
    "        out = self.bn2(out)\n",
    "        out += out_residual\n",
    "        \n",
    "        out = self.conv4(out)\n",
    "        out += residual\n",
    "        \n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "resident-lafayette",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
